// pages/shopInformation/shopInformation.js
const app = getApp();
import httpsUtils from '../../utils/https.js'; 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [], 
    goodsLength: 1, 
    current: 1,
    hasNextPage: true,
  },
  /**
   * 获取商品详情
   */
  gotoDetail: function (e) {
    var that = this;
    var goodsId = e.currentTarget.dataset.goodsid;
    var formatCare = e.currentTarget.dataset.goodscare;
    var userInfo = wx.getStorageSync("userInfo")
    var bullInfo = wx.getStorageSync("bullInfo");
    var shopInfo = wx.getStorageSync("shopInfo");
    if (formatCare == 1 && bullInfo == null && userInfo.id != shopInfo.userId) {
      wx.showModal({
        title: '提示',
        content: '查看保版商品，请下载服装基地APP',
        showCancel: false
      })
    } else {
      wx.navigateTo({
        url: '../productDetail/productDetail?id=' + goodsId,
      })
    }
  },
  /**
  * 获取商品
  */
  getGoodsBySort: function (e) {
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/goods/selectGoodsList?shopId=' + that.data.shopId + "&sortTypeId=" + that.data.sortTypeId + "&current=" + that.data.current,
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        var goodsList = json.data.list;
        var userInfo = wx.getStorageSync("userInfo");
        var bullInfo = wx.getStorageSync("bullInfo");
        var shopInfo = wx.getStorageSync("shopInfo");
        for (let obj of goodsList) {
          if (obj.formatCare == 1 && bullInfo == null && userInfo.id != shopInfo.userId) {
            obj.coverUrl = obj.coverUrl + '?x-oss-process=image/blur,r_50,s_50'
          }
        }
        that.setData({
          goodsList: that.data.goodsList.concat(goodsList),
          current: json.data.pageNum,
          hasNextPage: json.data.hasNextPage,
          goodsLength: json.data.total
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      sortTypeId: options.sortTypeId,
      shopId: options.shopId,
    })

    //获取商品
    that.getGoodsBySort();
  },
  // 事件函数

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasNextPage) {
      that.setData({
        current: that.data.current + 1,
      })
      that.getGoodsBySort();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
 
  /**
    * 生命周期函数--监听页面加载
    */
  onPageScroll: function (event) {
  },
 
})